﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EF.Util;
using EF.Util.DBUtility;
using EF.Model;
using EF.Model.Base;

namespace EF.DAL.Base
{
    public class FunctionDAO
    {
        /// <summary>
        /// 根据角色和语言获取页面的功能点
        /// </summary>
        /// <param name="role">系统角色</param>
        /// <param name="pageId">页面Id</param>
        /// <param name="lang">语言</param>
        /// <returns>e.g(refresh:'刷新',new:'新建')</returns>
        public string GetPageFunctionPoints(string role, int pageId, string lang)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@role", SqlDbType.NVarChar, 30),
                                           new SqlParameter("@pageId", SqlDbType.Int, 4),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@functionPoints", SqlDbType.NVarChar, 300)
                                       };

            sqlparams[0].Value = role;
            sqlparams[1].Value = pageId;
            sqlparams[2].Value = lang;
            sqlparams[3].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBasePageFunctionPointsGet", sqlparams);

            return sqlparams[3].Value.ToString();
        }

        /// <summary>
        /// 获取功能点树
        /// </summary>
        /// <param name="lang">语言</param>
        /// <returns></returns>
        public DataTable GetFunctionTree(string lang)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10)
                                       };

            sqlparams[0].Value = lang;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseFunctionTree", sqlparams);
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();

            return dt;
        }

        /// <summary>
        /// 功能新建
        /// </summary>
        /// <param name="model"></param>
        /// <param name="lang"></param>
        /// <param name="msg"></param>
        /// <returns>1,成功; 0,失败.</returns>
        public int Add(FunctionModel model, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@funcName_cn", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@funcName_en", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@funcName_ja", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@descr", SqlDbType.NVarChar, 200),
                                           new SqlParameter("@href", SqlDbType.NVarChar, 500),
                                           new SqlParameter("@icon", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@funcType", SqlDbType.Int, 4),
                                           new SqlParameter("@fatherId", SqlDbType.Int, 4),
                                           new SqlParameter("@createUser", SqlDbType.Int, 4),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@msg", SqlDbType.NVarChar, 300),
                                           new SqlParameter("@result", SqlDbType.Int, 4)
                                       };

            sqlparams[0].Value = model.FuncNameCn;
            sqlparams[1].Value = model.FuncNameEn;
            sqlparams[2].Value = model.FuncNameJa;
            sqlparams[3].Value = model.Descr;
            sqlparams[4].Value = model.URL;
            sqlparams[5].Value = model.Icon;
            sqlparams[6].Value = model.FuncType;
            sqlparams[7].Value = model.FatherId;
            sqlparams[8].Value = model.CreateUser;
            sqlparams[9].Value = lang;
            sqlparams[10].Direction = ParameterDirection.Output;
            sqlparams[11].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBaseFunctionAdd", sqlparams);

            msg = sqlparams[10].Value.ToString();

            return (int)sqlparams[11].Value;
        }

        /// <summary>
        /// 功能编辑
        /// </summary>
        /// <param name="model"></param>
        /// <param name="lang"></param>
        /// <param name="msg"></param>
        /// <returns>1,成功; 0,失败.</returns>
        public int Edit(FunctionModel model, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@funcName_cn", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@funcName_en", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@funcName_ja", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@descr", SqlDbType.NVarChar, 200),
                                           new SqlParameter("@href", SqlDbType.NVarChar, 500),
                                           new SqlParameter("@icon", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@funcType", SqlDbType.Int, 4),
                                           new SqlParameter("@fatherId", SqlDbType.Int, 4),
                                           new SqlParameter("@updateUser", SqlDbType.Int, 4),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@msg", SqlDbType.NVarChar, 300),
                                           new SqlParameter("@result", SqlDbType.Int, 4),
                                           new SqlParameter("@funcId", SqlDbType.Int, 4),
                                           new SqlParameter("@sortIndex", SqlDbType.Int, 4)
                                       };

            sqlparams[0].Value = model.FuncNameCn;
            sqlparams[1].Value = model.FuncNameEn;
            sqlparams[2].Value = model.FuncNameJa;
            sqlparams[3].Value = model.Descr;
            sqlparams[4].Value = model.URL;
            sqlparams[5].Value = model.Icon;
            sqlparams[6].Value = model.FuncType;
            sqlparams[7].Value = model.FatherId;
            sqlparams[8].Value = model.UpdateUser;
            sqlparams[9].Value = lang;
            sqlparams[10].Direction = ParameterDirection.Output;
            sqlparams[11].Direction = ParameterDirection.Output;
            sqlparams[12].Value = model.FuncId;
            sqlparams[13].Value = model.SortIndex;

            DbHelperSQL.RunProcedure("spBaseFunctionEdit", sqlparams);

            msg = sqlparams[10].Value.ToString();

            return (int)sqlparams[11].Value;
        }

        /// <summary>
        /// 获取Model
        /// </summary>
        /// <param name="funcId">功能Id</param>
        /// <returns></returns>
        public FunctionModel GetModel(int funcId)
        {
            SqlParameter[] sqlparams = {
                                          
                                           new SqlParameter("@funcId", SqlDbType.Int)
                                       };

            sqlparams[0].Value = funcId;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseFunctionModel", sqlparams);

            FunctionModel model = null;

            while (dr.Read())
            {
                model = new FunctionModel();

                model.FuncId = Convert.ToInt32(dr["FuncId"].ToString());
                model.FuncNameCn = dr["FuncName_cn"].ToString();
                model.FuncNameEn = dr["FuncName_en"].ToString();
                model.FuncType = Convert.ToInt32(dr["FuncType"].ToString());

                if (dr["FuncName_ja"] != null)
                {
                    model.FuncNameJa = dr["FuncName_ja"].ToString();
                }

                if (dr["Descr"] != null)
                {
                    model.Descr = dr["Descr"].ToString();
                }

                if (dr["Href"] != null)
                {
                    model.URL = dr["Href"].ToString();
                }

                if (dr["Icon"] != null)
                {
                    model.Icon = dr["Icon"].ToString();
                }

                if (dr["SortIndex"] != null && dr["SortIndex"].ToString() != "")
                {
                    model.SortIndex = Convert.ToInt32(dr["SortIndex"].ToString());
                }
            }

            return model;
        }

        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="funcId"></param>
        /// <param name="lang"></param>
        /// <param name="msg"></param>
        /// <returns>1,成功; 0,失败.</returns>
        public int Delete(int funcId, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@funcId", SqlDbType.Int, 4),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@msg", SqlDbType.NVarChar, 300),
                                           new SqlParameter("@result", SqlDbType.Int, 4)
                                       };

            sqlparams[0].Value = funcId;
            sqlparams[1].Value = lang;
            sqlparams[2].Direction = ParameterDirection.Output;
            sqlparams[3].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBaseFunctionDel", sqlparams);

            msg = sqlparams[2].Value.ToString();

            return (int)sqlparams[3].Value;
        }
    }
}
